package com.exam.sacrifice.ui.mine;

import static com.exam.commonbiz.util.AvatarUtils.REQUEST_CAMERA;
import static com.exam.commonbiz.util.AvatarUtils.tempCameraFile;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.exam.commonbiz.base.BaseActivity;
import com.exam.commonbiz.base.BaseViewModel;
import com.exam.commonbiz.util.AvatarUtils;
import com.exam.commonbiz.util.OnFastClickListener;
import com.exam.sacrifice.R;
import com.exam.sacrifice.databinding.ActivityUserInfoBinding;
import com.exam.sacrifice.util.BitmapCacheUtils;

import java.io.File;

public class UserInfoActivity extends BaseActivity<BaseViewModel, ActivityUserInfoBinding> implements AvatarUtils.OnAvatarSelectedListener {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_user_info;
    }

    @Override
    public void initData() {

        binding.include.tvTitle.setText("用户信息");
        binding.include.ivback.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                finish();
            }
        });

        binding.ivAvatar.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                AvatarUtils.showAvatarDialog(UserInfoActivity.this);
            }
        });

        Bitmap bitmap = BitmapCacheUtils.loadBitmapFromCache(context, "avatar.png");
        if (bitmap != null) {
            binding.ivAvatar.setImageBitmap(bitmap);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
            // data 为 null 是正常现象，直接从我们指定的路径获取照片
            if (tempCameraFile != null && tempCameraFile.exists()) {
                Bitmap bitmap = BitmapFactory.decodeFile(tempCameraFile.getAbsolutePath());
                binding.ivAvatar.setImageBitmap(bitmap);
                BitmapCacheUtils.saveBitmapToCache(context, bitmap, "avatar.png");
            } else {
                Toast.makeText(this, "照片获取失败", Toast.LENGTH_SHORT).show();
            }
            return;
        }
        Uri sourceUri = data.getData();
        if (sourceUri == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            Bitmap bitmap = AvatarUtils.getBitmapFromMediaStore(context, sourceUri);
            if (bitmap != null) {
                binding.ivAvatar.setImageBitmap(bitmap);
                BitmapCacheUtils.saveBitmapToCache(context, bitmap, "avatar.png");
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        AvatarUtils.onRequestPermissionsResult(this, requestCode, permissions, grantResults, this);
    }

    @Override
    public void onAvatarSelected(File avatarFile) {
        Log.d("zdddz", avatarFile.getAbsolutePath());
        // 加载头像
//        Glide.with(this)
//                .load(avatarFile)
//                .into(binding.ivAvatar);
        // TODO: 上传头像到服务器
    }
}